﻿using System;
using System.Linq;
using System.Linq.Expressions;
using NHibernate.Linq;
using SellAndBuy.Application.Query.DTOs;
using SellAndBuy.Application.Query.Finders;
using SellAndBuy.Infrastructure.Data.Core;
using SellAndBuy.Infrastructure.Data.Core.Finders;

namespace SellAndBuy.Infrastructure.Data.Query.Finders
{
    /// <summary>
    /// Ad Finder
    /// </summary>
    public class AdFinder : Finder<AdDTO>, IAdFinder
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="unitOfWork"></param>
        public AdFinder(INhibernateUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }

        /// <summary>
        /// Eager fetch of pics to avoid N+1 select problem
        /// </summary>
        /// <returns>Ads</returns>
        public override IQueryable<AdDTO> FindAll()
        {
            return base.FindAll().Fetch(ad => ad.Pictures);
        }

        /// <summary>
        /// Eager fetch of pics to avoid N+1 select problem
        /// </summary>
        /// <returns>Ads</returns>
        public override IQueryable<AdDTO> FindAll(int pageIndex, int pageCount)
        {
            return base.FindAll(pageIndex, pageCount).Fetch(ad => ad.Pictures);
        }

        /// <summary>
        /// Find all Ad for a category
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        //public IQueryable<Ad> FindByCategory(ItemCategory category)
        //{
        //    return base.FindBy(ad => ad.Item.Category == category).Fetch(ad => ad.Pictures);
        //}

        /// <summary>
        /// Find all Ad for a category
        /// </summary>
        /// <param name="category"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        //public IQueryable<Ad> FindByCategory(ItemCategory category, int pageIndex, int pageCount)
        //{
        //    return base.FindBy(ad => ad.Item.Category == category, pageIndex, pageCount).Fetch(ad => ad.Pictures);
        //}

        /// <summary>
        /// Eager fetch of pics to avoid N+1 select problem
        /// </summary>
        /// <param name="query">Query</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageCount">Page count</param>
        /// <returns>Ads</returns>
        public override IQueryable<AdDTO> FindBy(Expression<Func<AdDTO, bool>> query, int pageIndex, int pageCount)
        {
            return base.FindBy(query, pageIndex, pageCount).Fetch(ad => ad.Pictures);
        }

        /// <summary>
        /// Eager fetch of pics to avoid N+1 select problem
        /// </summary>
        /// <param name="query">Query</param>
        /// <returns>Ads</returns>
        public override IQueryable<AdDTO> FindBy(Expression<Func<AdDTO, bool>> query)
        {
            return base.FindBy(query).Fetch(ad => ad.Pictures);
        }

    }
}